/*
 * fcaStorePlace.java
 *
 * Created on 25. leden 2004, 22:48
 */

package cz.masa.FotoCAT;

import java.sql.*;
import java.util.*;
import java.io.*;

/**
 *
 * @author  Jiri Masnik
 */
public class fcaStorePlace {
    private int     db_id;
    private String  db_name;
    private String  db_path;
    private int     db_type;
    
    public static final String TYPE_NAME_UNKNOWN    =   "Unknown";
    public static final String TYPE_NAME_0_HDLAN    =   "Hard Drive / LAN";
    public static final String TYPE_NAME_1_CDDVD    =   "CD/DVD";
    public static final String TYPE_NAME_2_ZIP      =   "Iomega Zip";
    
    private static final String MAIN_SQL_FLDS = "fca02_store.fca02_id, fca02_store.fca02_name, fca02_store.fca02_path, fca02_store.fca02_type";
    public static String getMainSQLFlds(){ return fcaStorePlace.MAIN_SQL_FLDS; }
    private static final String MAIN_SQL = "SELECT " + fcaStorePlace.MAIN_SQL_FLDS + " FROM fca02_store";
    public static String getMainSQL(){ return fcaStorePlace.MAIN_SQL; }
    
    /** Creates a new instance of fcaStorePlace */
    public fcaStorePlace() {
        this.db_id = -1;
    }

    public fcaStorePlace(int db_id) {
        ResultSet rs = SQLCon.exSQLres(fcaStorePlace.MAIN_SQL + " WHERE fca02_store.fca02_id = " + db_id);
        try {
            rs.first();
            dbLoad(rs);
        } catch(SQLException ex) { }
    }
    
    public int dbLoad(ResultSet rs){
        try {
            this.db_id = rs.getInt("fca02_store.fca02_id");
            this.db_name = rs.getString("fca02_store.fca02_name");
            this.db_path = rs.getString("fca02_store.fca02_path");
            this.db_type = rs.getInt("fca02_store.fca02_type");
            return 1;
        }
        catch(SQLException ex) {
            return 0;
        }
    }
    
    public void dbDelete(){
        if(db_id != -1){
            SQLCon.exSQL("DELETE FROM fca02_store WHERE fca02_id = " + db_id);
        }
    }
    
    public void dbSave(){
        db_path = FotoCATutil.str_replace(db_path, "\\", "\\\\");
        if(db_type > 2 || db_type < 0) db_type = 0;
        
        if(db_id == -1){
            String sql = "INSERT INTO fca02_store (fca02_name, fca02_type, fca02_path) VALUES ('" + db_name + "', " + db_type + ",'" + db_path + "')";
            SQLCon.exSQL(sql);
            // WARNING - after insert it don't have right ID in db_id
        } else {
            String sql = "UPDATE fca02_store SET fca02_name = '" + db_name + "', fca02_path = '" + db_path + "', fca02_type = " + db_type + " WHERE fca02_id = " + db_id;
            SQLCon.exSQL(sql);
        }
    }

    /**
     * Seznam fyzickych adresaru v danem ukladacim miste
     */
    public Vector<String> getFilesystemDirs(boolean hide_existing){
        Vector<String> ret = new Vector<String>(20);

        File dir = new File(this.getFullPath());
        
        if(dir.exists() == true){
            String ple[] = dir.list();

            for(int i=0; i<ple.length; i++){
                File fl = new File(dir.getAbsolutePath() + FotoCAT.file_sep + ple[i]);
                if(fl.isDirectory()){
                    boolean add = true;
                    
                    // odfiltrovani tech, ktere uz jsou pridane
                    if(hide_existing){
                        String sql = "SELECT fca04_id AS id FROM fca04_directory WHERE fca02_id = " + this.db_id + " AND fca04_directory = '" + ple[i] + "'";
                        ResultSet rs = SQLCon.exSQLres(sql);
                        try {
                            if(rs.first()) add = false;
                        } catch(SQLException ex) {
                            System.err.println(ex);
                        }
                    }
                    
                    // pridani do vysledku
                    if(add) ret.add(ple[i]);
                }
            }
        }
        return ret;
    }
    
    /**
     * Cesta k danemu ukladacimu mistu
     */
    public String getFullPath(){
        String ret_path = db_path;
        if(db_type == 1) ret_path = FotoCAT.applicationProps.getProperty("CDDrive") + ret_path;
        if(db_type == 2) ret_path = FotoCAT.applicationProps.getProperty("ZipDrive") + ret_path;
        return ret_path;
    }
    
    public String getName(){ return db_name; }
    public void setName(String name){ db_name = name; }
    
    public String getPath(){ return db_path; }
    public void setPath(String path){ db_path = path; }
    
    public int getTypeId(){ return db_type; }
    public void setTypeId(int type){ db_type = type; }
    
    public int getId(){ return db_id; }
    
    public String getTypeStr(){
        if(db_type == 0) return  TYPE_NAME_0_HDLAN;
        if(db_type == 1) return  TYPE_NAME_1_CDDVD;
        if(db_type == 2) return  TYPE_NAME_2_ZIP;
        return TYPE_NAME_UNKNOWN;   
    }
    
    public long getDataAmount(){
        long ret;
        ResultSet rs = SQLCon.exSQLres("SELECT SUM(pictures.filesize) AS size FROM pictures, dirs, fca02_store WHERE pictures.dir = dirs.id AND dirs.umisteni = fca02_store.fca02_id AND fca02_store.fca02_id =" + db_id);
        try {
            rs.first();
            ret = rs.getLong("size");
        } catch(SQLException ex) {
            return 0;
        }
        return ret;
    }
    
    public int getDirCount(){
        ResultSet rs = SQLCon.exSQLres("SELECT COUNT(*) AS count FROM fca04_directory WHERE fca02_id = " + db_id);
        try {
            rs.first();
            return rs.getInt("count");
        }
        catch(SQLException ex) {
            return -1;
        }
    }
    
    public String toString(){
        return db_name + " (" + getTypeStr() + ")";
    }
    
    /**
     * Seznam vsech ukladacich mistecek
     */
    public static Vector<fcaStorePlace> getAllStorePlaces(){
        Vector<fcaStorePlace> ret = new Vector<fcaStorePlace>(50);
        ResultSet rs = SQLCon.exSQLres(fcaStorePlace.MAIN_SQL + " ORDER BY fca02_store.fca02_name");
        try {
            while(rs.next()){
                fcaStorePlace place = new fcaStorePlace();
                place.dbLoad(rs);
                ret.add(place);
            }
        }
        catch(SQLException ex) { }
        return ret;
    }
}